package code.module.motion
{
	
	import code.app.AppData;
	import code.app.AppDmList;
	import code.app.AppView;
	import code.main.controller.event.EventManager;
	import code.main.controller.event.EventName;
	import code.main.controller.event.MyEvent;
	import code.main.modle.table.Player;
	import code.net.SocketManager;
	
	import com.fminutes.vinciIsometric.events.PositionEvent;
	import com.yy.manager.ResourceManager;
	import com.yy.tools.arry.ArrayTools;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.net.getClassByAlias;
	import flash.ui.Mouse;
	
	/**
	 * 表情管理器
	 * @author YY
	 * 
	 */
	public class FaceEmotionManager
	{
		private var _emotionPanel:MotionPanel;//表情面板
		private var _container:DisplayObjectContainer;//表情模块父级模块
		private var _pageSize:int=7;//每页显示表情数
		public function FaceEmotionManager(parent:DisplayObjectContainer)
		{
			this._container = parent;
			init();
		}
		
		/**
		 *初始化表情资源 
		 * 
		 */
		private function init():void {
			_emotionPanel = new MotionPanel();
			var dobj:DisplayObject;
			addEvent();
			dobj = _container.addChild(_emotionPanel);
			dobj.x = 480;
			dobj.y = 603;
			showMotionPanle(false);
		}
		
		/**
		 *侦听事件 
		 * 
		 */
		private function addEvent():void {
			this._emotionPanel.motionBtn.addEventListener(MouseEvent.CLICK, clickSwitchBtnFunc);
			EventManager.getInstance().addEventListener(EventName.MOTION_RECEIVE_MOTION, recEmotion);
			EventManager.getInstance().addEventListener(EventName.MOTION_OVER_MOTION, overEmotion);
			EventManager.getInstance().addEventListener(EventName.MOTION_OUT_MOTION, outEmotion);
			EventManager.getInstance().addEventListener(EventName.MOTION_CLICK_MOTION, clickEmotion);
		}
		
		/**
		 *鼠标移动到表情上 
		 * @param e
		 * 
		 */
		private function overEmotion(e:MyEvent):void {
			var id:int = e.intData;
			var vo:MotionVo = MotionDm.getInstance().getMotionVoById(id);
			_emotionPanel.preMotion = vo.preShowMotion;
			vo.preShowMotion.gotoAndStop(1);
		}
		/**
		 *鼠标移开表情  
		 * @param e
		 * 
		 */
		private function outEmotion(e:MyEvent):void {
			var id:int = e.intData;
			var vo:MotionVo = MotionDm.getInstance().getMotionVoById(id);
			_emotionPanel.preMotion = null;
			vo.preShowMotion.gotoAndStop(2);
		}
		/**
		 *点击表情 
		 * @param e
		 * 
		 */
		private function clickEmotion(e:MyEvent):void {
			clickSwitchBtnFunc(null);
			var id:int = e.intData;
			var vo:MotionVo = MotionDm.getInstance().getMotionVoById(id);
			_emotionPanel.preMotion = null;
			vo.preShowMotion.gotoAndStop(2);
			sendEmotion(String(id));
		}
		
		/**
		 *点击表情开关 
		 * @param e
		 * 
		 */
		private function clickSwitchBtnFunc(e:Event):void {
			showMotionPanle(!this._emotionPanel.motionContainer.visible);
		}
		/**
		 *控制表情面板显示隐藏 
		 * 
		 */
		private function showMotionPanle(show:Boolean):void {
			this._emotionPanel.motionContainer.visible = show;
			showMotionPicList(1);
		}
		
		/**
		 *显示表情列表 
		 * 
		 */
		private function showMotionPicList(page:int):void {
			var list:Array = MotionDm.getInstance().getPageList(page, _pageSize);
			_emotionPanel.listDG.clear();
			var i:int;
			var len:int = list.length;
			var mc:MovieClip;
			var vo:MotionVo;
			for(i=0; i<len; i++) {
				vo = list[i];
				mc = vo.motionPic;
				_emotionPanel.listDG.setGrid(mc, 0, i);
			}
			_emotionPanel.listDG.update();
		}
		
		
		/**
		 *播放动画 
		 * 
		 */
		private function playMotion(motionId:int, seatId:int):void {
			var pos:Point = MotionDm.getInstance().getMotionPosBySeatId(seatId);
			var motion:MovieClip = MotionDm.getInstance().getShowMotionById(motionId);
			var dobjc:DisplayObjectContainer = AppView.getInstance().motionLayer;
			dobjc.addChild(motion);
			motion.x =pos.x;
			motion.y = pos.y;
			motion.addEventListener(Event.ENTER_FRAME, motionFrameFunc);
			motion.play();
		}
		
		/**
		 *移除mc 
		 * @param e
		 * 
		 */
		private function motionFrameFunc(e:Event):void {
			var mc:Object = e.currentTarget;
			if(mc.currentFrame == mc.totalFrames) {
				mc.stop();
				mc.removeEventListener(Event.ENTER_FRAME, motionFrameFunc);
				var dobjc:DisplayObjectContainer = AppView.getInstance().motionLayer;
				if(dobjc.contains(DisplayObject(mc))) {
					dobjc.removeChild(DisplayObject(mc));
				}
			}
		}
		
		/**
		 *发送表情 
		 * @param emotionId 表情的id
		 * 
		 */
		public function sendEmotion(emotionId:String):void {
			var sm:SocketManager = SocketManager.getInstance();
			sm.sendExpression(int(emotionId));
		}
		
		/**
		 * 接收表情
		 * @param userId   发送表情的用户id
		 * @param motionId 表情的id
		 * 
		 */
		public function recEmotion(e:MyEvent):void {
			var uid:String = e.objData.uid;
			var motionId:int = e.objData.motionId;
			trace("收到表情：userId-" + uid + "<:>motionId-" + motionId);
			var p:Player = AppDmList.getInstance().tableData.getMatchPlyerById(int(uid));
			if(p) {
				var seatId:int = p.seatId;
				playMotion(motionId, seatId);
			}
		}
	}
}